package top.lywivan.netty.handler.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import top.lywivan.netty.pojo.UserInfo;

/**
 * @author :lyw.ivan
 * @date :Created in 2022/8/14
 * @description : 处理业务数据，具体为读取客户端发送的数据
 * @version: 1.0
 */
@Slf4j
public class TcpStickHalfHandler1 extends SimpleChannelInboundHandler<UserInfo> {

    //统计某一客户端请求次数
    int count=0;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, UserInfo userInfo) throws Exception {
        count++;
        log.info("---服务端接受到第{}个数据：{}",count,userInfo);
    }

    /**
     * 读完之后给客户端响应个消息
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        UserInfo userInfo=new UserInfo();
        userInfo.setName("服务端读完了消息。。。");
        ctx.writeAndFlush(userInfo);
        super.channelReadComplete(ctx);
    }
}
